//
//  HomePageMgr.m
//  carcareIOS
//
//  Created by ileo on 16/3/21.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "HomePageMgr.h"
#import "NetHome.h"

@implementation HomePageMgr

+(void)getAllRewardFinish:(void (^)(NSInteger))finish fail:(void (^)())fail{
    
    [[NetHome getRewardDetail] requestSuccess:^(id JSON) {
        NSArray *datas = JSON[@"info_list"];
        
        __block dispatch_semaphore_t semap = dispatch_semaphore_create(0);
        __block BOOL isFail = NO;
        __block NSInteger money = 0;
        
        dispatch_async(dispatch_queue_create("getallreward", NULL), ^{
            
            for (NSDictionary *dic in datas) {
                
                if (![dic[@"status"] boolValue]) {
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [[NetHome getShakeMoneyWithInfoID:dic[@"info_id"]] requestSuccess:^(id JSON) {
                            dispatch_semaphore_signal(semap);
                            money += [JSON[@"num"] integerValue];
                        } failure:^(id JSON) {
                            dispatch_semaphore_signal(semap);
                            isFail = YES;
                        }];
                    });
                    
                    dispatch_semaphore_wait(semap, DISPATCH_TIME_FOREVER);
                    
                }
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                if (isFail) {
                    if (fail) {
                        fail();
                    }
                }else{
                    if (finish) {
                        finish(money);
                    }
                }

            });
            
        });

     
    } failure:^(id JSON) {
        
        if (fail) {
            fail();
        }
        
    }];
    

}


@end
